package com.stockz.watchlist.manager;

import java.util.List;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.stockz.core.model.Scrip;
import com.stockz.watchlist.dao.WatchListDao;
import com.stockz.watchlist.model.WatchList;
import com.stockz.watchlist.model.WatchListEntry;

@Component
@Transactional
public class WatchListManager extends Observable{

	@Autowired 
	private WatchListDao watchListDao;
	
	private List<WatchList> allWatchListList;
	
	public void validate(WatchList watchList){
		if(watchList == null) throw new IllegalArgumentException("");
		if(!StringUtils.hasText(watchList.getName())) throw new IllegalArgumentException(""); 
	}
	
	public void validate(WatchListEntry watchListEntry){
		if(watchListEntry == null)	throw new IllegalArgumentException("WatchListEntry is null");
		if(watchListEntry.getWatchList() == null)	throw new IllegalArgumentException("WatchList for WatchListEntry is null");
		if(watchListEntry.getInspectionDate() == null) 	throw new IllegalArgumentException("Inspection Date for WatchListEntry is null");
		if(watchListEntry.getScrip() == null)	throw new IllegalArgumentException("Scrip for WatchListEntry is null");
		if(!StringUtils.hasText(watchListEntry.getScrip().getCode()))	throw new IllegalArgumentException("Scrip code for WatchListEntry is null or empty");
	}
	
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void create(WatchList watchList){
		validate(watchList);
		watchListDao.persist(watchList);
		allWatchListList.add(watchList);
		setChanged();
		notifyObservers();
	}
	
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void create(WatchListEntry watchListEntry){
		validate(watchListEntry);
		watchListDao.persist(watchListEntry);
	}
	
	@Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=true)
	public List<WatchList> getAll(){
		if(allWatchListList == null || allWatchListList.isEmpty()){
			allWatchListList = watchListDao.getAll();
		}
		return allWatchListList;
	}
	
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public List<WatchListEntry> getByWatchListId(Long id){
		return watchListDao.getByWatchListId(id);
	}
	
	@Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=true)
	public List<Scrip> getScripList(Long watchListId){
		if(watchListId == null || watchListId == 0) throw new IllegalArgumentException("WatchListId is null or 0");
		return watchListDao.getScripList(watchListId);
	}
	
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void remove(Long watchListId){
		if(watchListId == null || watchListId == 0) throw new IllegalArgumentException("WatchListId is null or 0");
		watchListDao.remove(watchListId);
		for(WatchList watchList : allWatchListList){
			if(watchList.getId().equals(watchListId)){
				allWatchListList.remove(watchList);
			}
		}
	}
}
